package com.tanhua.template;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import com.tanhua.properties.BaiduAipProperties;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

public class BaiDuAipTemplate {


    private BaiduAipProperties properties;
    private AipFace client;

    public BaiDuAipTemplate(BaiduAipProperties properties) {
        this.properties = properties;
         client = new AipFace(properties.getAppId(), properties.getApiKey(), properties.getSecretKey());

    }

    public Boolean HeaderAip(byte[] body){
        // 初始化一个AipFace
        HashMap<String, String> options = new HashMap<>();
        // 要检测的图片数据，使用百度提供的Base64Util，把图片字节数组按照base64进行编码转换
        String imageType = "BASE64";
        String image = Base64Util.encode(body);

        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);
        System.out.println(res);
        int errorCode = res.getInt("error_code");
        if (errorCode==0){
            int faceNum = res.getJSONObject("result").getInt("face_num");
            return faceNum > 0;
        }
        return false;
    }
}
